
package com.example.demo.modules.job.controller;

import com.example.demo.core.entity.ProcessResult;
import com.example.demo.core.result.PageResult;
import com.example.demo.modules.job.constants.Constant;
import com.example.demo.modules.job.model.SysTask;
import com.example.demo.modules.job.service.JobService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import static com.example.demo.core.entity.ProcessResult.ERROR;
import static com.example.demo.core.entity.ProcessResult.SUCCESS;

/**
 * 定时任务
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.2.0 2016-11-28
 */
@RestController
@RequestMapping("/data/schedule")
public class ScheduleJobController {
	@Autowired
	private JobService scheduleJobService;


	@RequestMapping("/list")
	public ModelAndView list() {
		return new ModelAndView("/modules/task/list");
	}

	@RequestMapping("/form")
	public ModelAndView form() {
		return new ModelAndView("/modules/task/form");
	}
	
	/**
	 * 定时任务列表
	 */
	@RequestMapping("/pages")
	//@RequiresPermissions("sys:schedule:list")
	public PageResult list(SysTask SysTask){
		List<SysTask> page = scheduleJobService.queryPage(SysTask);
		return new PageResult(page);
	}
	
	/**
	 * 定时任务信息
	 */
	@RequestMapping("/info/{id}")
	//@RequiresPermissions("sys:schedule:info")
	public ProcessResult info(@PathVariable("id") Long id){
		SysTask schedule = scheduleJobService.get(id);
		
		return new ProcessResult(schedule);
	}
	
	/**
	 * 保存定时任务
	 */
	//@SysLog("保存定时任务")
	@RequestMapping("/saveOrUpdate")
	//@RequiresPermissions("sys:schedule:save")
	public ProcessResult saveOrUpdate(SysTask scheduleJob){
		if(scheduleJob.getId()!=null){
			scheduleJobService.update(scheduleJob);
		}else{
			scheduleJobService.save(scheduleJob);
		}
		return new ProcessResult();
	}
	
	/**
	 * 修改定时任务
	 *//*
	//@SysLog("修改定时任务")
	@RequestMapping("/update")
	//@RequiresPermissions("sys:schedule:update")
	public ProcessResult update(@RequestBody SysTask scheduleJob){
		scheduleJobService.update(scheduleJob);
		
		return new ProcessResult();
	}*/
	
	/**
	 * 批量删除定时任务
	 */
	//@SysLog("删除定时任务")
	@RequestMapping("/delete")
	//@RequiresPermissions("sys:schedule:delete")
	public ProcessResult delete(@RequestParam("ids[]")Long[] ids){
		scheduleJobService.batchRemove(ids);
		
		return new ProcessResult();
	}

	/**
	 * 删除定时任务
	 */
	@RequestMapping("/deleteOnlyOne/{id}")
	//@RequiresPermissions("sys:schedule:delete")
	public ProcessResult deleteOnlyOne(@PathVariable Long id){
		scheduleJobService.remove(id);

		return new ProcessResult();
	}
	
	/**
	 * 立即执行任务
	 */
	//@SysLog("立即执行任务")
	/*@RequestMapping("/run")
	//@RequiresPermissions("sys:schedule:run")
	public ProcessResult run(@RequestParam("ids[]")Long[] ids){
		scheduleJobService.run(ids);
		
		return new ProcessResult();
	}*/
	
	/**
	 * 暂停定时任务
	 */
	//@SysLog("暂停定时任务")
	/*@RequestMapping("/pause")
	//@RequiresPermissions("sys:schedule:pause")
	public ProcessResult pause(@RequestParam("ids[]")Long[] ids){
		scheduleJobService.pause(ids);
		
		return new ProcessResult();
	}*/
	
	/**
	 * 恢复定时任务
	 */
	//@SysLog("恢复定时任务")
	/*@RequestMapping("/resume")
	//@RequiresPermissions("sys:schedule:resume")
	public ProcessResult resume(@RequestParam("ids[]")Long[] ids){
		scheduleJobService.resume(ids);
		
		return new ProcessResult();
	}*/

	@RequestMapping(value = "/changeJobStatus")
	public ProcessResult changeJobStatus(Long id,String cmd ) {
		String label = "停止";
		if ("start".equals(cmd)) {
			label = "启动";
		}
		try {
			scheduleJobService.changeStatus(id, cmd);
			return new ProcessResult(SUCCESS,"任务" + label + "成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ProcessResult(ERROR,"任务" + label + "失败");
	}

}
